#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "Marker.h"
#include "GraphicsScene.h"
#include <QtGui>

const int IdRole = Qt::UserRole;
int Gsize;
int Gshp_inx;
QColor col;
int rbID;


MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Escene=new GraphicsScene();  //Sets up Scene to use in QGraphicsView
    Escene->setSceneRect(QRect(0, 0, 661, 381));

    VEscene=new QGraphicsScene();  //Sets up Scene to use in QGraphicsView
    VEscene->setSceneRect(QRect(0, 0, 461, 301));

    Sscene=new GraphicsScene();  //Sets up Scene to use in QGraphicsView
    Sscene->setSceneRect(QRect(0, 0, 661, 381));

    Eview = new QGraphicsView(ui->ECSView);            //Creates pointer to ECSView ui element
    VEview = new QGraphicsView(ui->VSECSView);            //Creates pointer to ECSView ui element
    Sview=new QGraphicsView(ui->SigfriedView);         //Creates pointer to SigfriedView ui element



    Eview->setScene(Escene);
    Eview->setDragMode(QGraphicsView::RubberBandDrag);  //Allows you to select multiple items click-dragging the cursor
    Eview->setRenderHints(QPainter::Antialiasing);      //Enable antialiasing

    VEview->setScene(VEscene);
    VEview->setRenderHints(QPainter::Antialiasing);      //Enable antialiasing

    Sview->setScene(Sscene);
    Sview->setDragMode(QGraphicsView::RubberBandDrag);  //Allows you to select multiple items click-dragging the cursor
    Sview->setRenderHints(QPainter::Antialiasing);      //Enable antialiasing

    //Read Default values
    Gsize=ui->SizeSpinBox->value();
    Gshp_inx=ui->ShapeMenu->currentIndex();
    rbID=radioButtonSelected();

    switch(rbID)
    {
    case 1:col=Qt::yellow;
        break;
    case 2:col=Qt::darkGreen;
        break;
    case 3:col=Qt::darkBlue;
        break;
    case 4:col=QColor(255,140,0,255);
        break;
    case 5:col=Qt::magenta;
        break;
    case 6:col=Qt::black;
        break;
    case 7:col=Qt::white;
        break;
    }


}


MainWindow::~MainWindow()
{
    delete ui;
    delete Escene;
    delete Eview;
    delete Sscene;
    delete Sview;

}



void MainWindow::on_ImageSelectButton1_clicked()
{
    QString fileName1 = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath()); //Open File Dialog


    if (!fileName1.isEmpty())                    //Check to see if a file is selected. If a file is selected pass image create QImage object
    {
            QImage image1(fileName1);             //Load Image

            if (image1.isNull())                 //If not an image file display error
            {
                QMessageBox::information(this, tr("Sigfried Viewer"), tr("Cannot load %1.").arg(fileName1));

                return;
            }

            //view = new QGraphicsView(ui->ECSView); //Creates pointer to ECSView ui element

            QPixmap img1 = QPixmap::fromImage(image1);
            Sscene->setBackgroundBrush(img1.scaled(661,381,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
            Sview->setScene(Sscene);


            Sview->setGeometry(QRect(0, 0, 665, 385));
            Sview->show();

     }

}


void MainWindow::on_ImageSelectButton2_clicked()
{

    QString fileName2 = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath()); //Open File Dialog


    if (!fileName2.isEmpty())                    //Check to see if a file is selected. If a file is selected pass image create QImage object
    {
            QImage image2(fileName2);             //Load Image

            if (image2.isNull())                 //If not an image file display error
            {
                QMessageBox::information(this, tr("ECS Viewer"), tr("Cannot load %1.").arg(fileName2));

                return;
            }

            //view = new QGraphicsView(ui->ECSView); //Creates pointer to ECSView ui element

            QPixmap img2 = QPixmap::fromImage(image2);
            Escene->setBackgroundBrush(img2.scaled(661,381,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
            Eview->setScene(Escene);

            Eview->setGeometry(QRect(0, 0, 665, 385));
            Eview->show();

           //VEscene->setBackgroundBrush(img2.scaled(461,301,Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation));
           // VEview->setScene(VEscene);

           //VEview->setGeometry(QRect(0, 0, 465, 305));
           //VEview->show();

     }


}



void MainWindow::on_YellowMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::yellow);

    }

    Eview->update();
    col=Qt::yellow;

}
void MainWindow::on_BlueMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::darkBlue);

    }

    Eview->update();
    col=Qt::darkBlue;

}
void MainWindow::on_GreenMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::darkGreen);

    }

    Eview->update();
    col=Qt::green;

}
void MainWindow::on_MagentaMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::magenta);

    }

    Eview->update();
    col=Qt::magenta;

}
void MainWindow::on_OrangeMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(QColor(255,140,0,255));

    }

    Eview->update();
    col=QColor(255,140,0,255);

}
void MainWindow::on_BlackMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::black);

    }

    Eview->update();
    col=Qt::black;

}
void MainWindow::on_WhiteMarker_clicked()
{

    Marker *marker;

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setOutlineColor(Qt::white);

    }

    Eview->update();
    col=Qt::white;

}



void MainWindow::on_ShapeMenu_activated(int index)
{
    Marker *marker;
    Marker::Shape shape = Marker::Shape(index-1);//ui->ShapeMenu->itemData(
                 //ui->ShapeMenu->currentIndex(), IdRole).toInt());

    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setShape(shape);
    }

    Eview->update();
    Gshp_inx=ui->ShapeMenu->currentIndex();
}

void MainWindow::on_SizeSpinBox_valueChanged(int index)
{
    Marker *marker;
    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setSize(ui->SizeSpinBox->value());
    }

    Eview->update();
    Gsize=ui->SizeSpinBox->value();
    Gshp_inx=ui->ShapeMenu->currentIndex();
}


short MainWindow::getSize()
{
    return Gsize;
}
QColor MainWindow::getColor()
{
    return col;
}


int MainWindow::getShape()
{
    return Gshp_inx;
}

int MainWindow::radioButtonSelected()
{
    if(ui->YellowMarker->isChecked())
        return 1;
    else if(ui->GreenMarker->isChecked())
        return 2;
    else if(ui->BlueMarker->isChecked())
        return 3;
    else if(ui->OrangeMarker->isChecked())
        return 4;
    else if(ui->MagentaMarker->isChecked())
        return 5;
    else if(ui->BlackMarker->isChecked())
        return 6;
    else if(ui->WhiteMarker->isChecked())
        return 7;
    else
        return 0;
}



void MainWindow::on_tabWidget_selected(const QString &arg1)
{
    Marker *marker;
    QList<QGraphicsItem *> items =Escene->items();
    QListIterator<QGraphicsItem *> i(items);

    while(i.hasNext())
    {
        marker=(Marker *)i.next();
        marker->setSelected(false);
    }

    Eview->update();

    QString fileName = "ECSview.png";
    QByteArray bytes;
    QBuffer buffer(&bytes);
    buffer.open(QIODevice::WriteOnly);
    QPixmap pixMap = QPixmap::grabWidget(Eview);

    VEscene->setBackgroundBrush(pixMap.scaled(461,301,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));

}
